package leetcode.weekly.week291;

import java.util.Arrays;

import leetcode.IDebug;

public class Solution6 implements IDebug {

	public long appealSum(String s) {
		char[] cs = s.toCharArray();
		int len = s.length();
		boolean[] hash = new boolean[26];
		Long[][] dp = new Long[len][len];
		return find(cs, 0, len - 1, hash, dp);
	}

	private long find(char[] cs, int L, int R, boolean[] hash, Long[][] dp) {
		if (L < 0 || R == cs.length || L > R) {
			return 0;
		}
		if (L == R) {
			int cur = cs[L] - 'a';
			hash[cur] = true;
			return 1;
		}
//		if (dp[L][R] != null) {
//			return dp[L][R];
//		}
		long ans = 0;
		boolean[] hash1 = new boolean[26];
		ans += this.find(cs, L + 1, R, hash1, dp);
		boolean[] hash2 = new boolean[26];
		ans += this.find(cs, L, R - 1, hash2, dp);
		long cnt = 0;
		for (int i = 0; i < hash1.length; i++) {
			if (hash1[i] || hash2[i]) {
				hash[i] = true;
				cnt++;
			}
		}
		ans -= this.find(cs, L + 1, R - 1, hash2, dp);
		ans += cnt;
		dp[L][R] = ans;
		return ans;
	}

	@Override
	public void debug4() {
		long ans = this.appealSum("abb");
		System.out.println(ans);
	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution6 so = new Solution6();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
